package com.liao.healthydiet.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.liao.healthydiet.GridViewFoodNLayoutAdapter;
import com.liao.healthydiet.R;
import com.liao.healthydiet.domain.Food;
import com.liao.healthydiet.utils.GetData;

import java.util.ArrayList;

public class FoodNutritionActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private GridView gridView;
    private ArrayList<Food> foods;
    private GridViewFoodNLayoutAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_foot_nutrition);
        initView();
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        initData();
    }

    private void initView() {
        gridView = (GridView) findViewById(R.id.gridView);

        foods = new ArrayList<>();
        adapter = new GridViewFoodNLayoutAdapter(this, foods);
        gridView.setAdapter(adapter);

        gridView.setOnItemClickListener(this);
    }

    private void initData() {
        String[] names = GetData.food;
        String[] collocations = GetData.food1;
        String[] details = GetData.foodjianjie;
        int[] images = GetData.resId;
        for (int i = 0; i < names.length; i++) {
            Food food = new Food();
            food.setName(names[i]);
            food.setCollocation(collocations[i]);
            food.setDetail(details[i]);
            food.setImage(images[i]);
            foods.add(food);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        startActivity(new Intent(this, FoodDetailActivity.class).putExtra("food", foods.get(position)));
    }
}
